
function toDate(dStr ) {
	var now = new Date(); 
 		now.setHours(dStr.substr(0,dStr.indexOf(":")));
 		now.setMinutes(dStr.substr(dStr.indexOf(":")+1, dStr.indexOf(":")));
 		now.setSeconds(dStr.substr(dStr.indexOf(":")+4));
 		return now;
	 
}
function getSecRemaining(time){
	 
	var date = new  Date();
	var ret = time.getTime()-date.getTime();
	return Math.abs( ret  );
}
function getMin(m){
	if(m<10)
		 return  "0"+m;
		return m;
}

function getSec(s){
	if(s<10)
	 return  "0"+s;
	return s;
}

function getIntervalRemaining(dStr){
	var time = toDate(dStr);
	var nr_sec= getSecRemaining(time);
	var d = new Date(nr_sec);
	
	 var min = getMin(d.getMinutes());
	 var sec= getSec(d.getSeconds());
	
	return min+":"+sec;
}


function setUpSchedueleList(){
	  schedueleList =Ext.StoreMgr.lookup("BussesTime").data.items;
	if(!listScheduelePanel)listScheduelePanel =  new Ext.Panel({
		 
    });
	dispatcher();
	 

}
 


function addScheduele2(scheduele){ 
	if(nowIsInRange(scheduele)){
		  putTimer(scheduele);
	  }
	  else {
		  putHour( scheduele);
	  }
};
function putTimer(scheduele){
	var late , hour ; 
			hour = scheduele.hour +"  "+ getIntervalRemaining(scheduele.hour);
	 
	if(busHasPassed(scheduele.hour)==true) late = "'red'";
	else late = "'green'"; 
	 var tmp ={
			 xtype: 'container',
	         items: [{
	         html: "<div id="+late+" class='hour_row'>" +
	         		    "<div  class='bus'> "  +scheduele.bus+"</div >" +
	         	       " <div  class='hour'>"+ hour+"</div>" +
	         	     "</div>"
	      }]
	    }; 
	 if(busIsGone(scheduele.hour)==false) listScheduelePanel.add(tmp); 
}

///Creates a container  to show the time busses come
function putHour(scheduele){ 
	 var tmp ={
			 xtype: 'container',
	         items: [{
	         html: "<div id='blue' class='hour_row'> " +
	         		    "<div  class='bus'> "  +scheduele.bus+"</div >" +
	         	       " <div  class='hour'>"+ scheduele.hour +"</div>" +
	         	     "</div>"
	      }]
	    }; 
	listScheduelePanel.add(tmp); 
}



function busIsGone(hour){
	var ct =   new Date();//current time
   var bh = toDate(hour); // bus hour
	var bh_ct = bh.getTime() - ct.getTime();
	 if( bh_ct<-5 * 60000) return true; 
	 return false;
}	

function busHasPassed(hour){
	var ct =   new Date();//current time
   var sh = toDate(hour); // specified hour
	var sh_ct = sh.getTime() - ct.getTime();
	 if( sh_ct<0) return true;
	 return false;
}
function nowIsInRange(){
	
	///has to check the day as well ,if  picker is saturday or sunday and now is week!!!
	    var pickedHour = pickerSelectedValue.time_hour+":"+getMin(pickerSelectedValue.time_minute)+":00";
	 if(dayIsCorrect()==true){
		var ct = new Date();
	    var st = toDate(pickedHour);
		var   ct_st = ct.getTime()-st.getTime();
		if(ct_st >-10*60000 && ct_st<45*60000) return true;
	}
		return false;
}


 function dayIsCorrect(){
	 var ct = new Date();
	 if(pickerSelectedValue.time_day==2&& ct.getDay()==6) 
		 	return true;
	 if(pickerSelectedValue.time_day==3&& ct.getDay()==0) 
		 return true; 
	 return false;
 }
 
 
 function 	addSchedueleToPanel(   ){
	 listScheduelePanel.removeAll();
		  for(var i=0; i< schedueleList.length ;i++){
			  addScheduele2(schedueleList[i].data);
			 
			  listScheduelePanel.setScrollable('vertical');
			  listScheduelePanel.doLayout();
		  }
		   
	  }


 function dispatcher(){
 	 
	 addSchedueleToPanel();
 	hoursInterval = setInterval ( 'addSchedueleToPanel()', 1000 );
 	// dis = clearInterval ( dis );
 }

Ext.regController('Scheduele',{
	//based, on the date chosen,bus stations and  busses chosen get data
	show: function(){
	
		 obj = this;
		
		
		 var currentHour = pickerSelectedValue.time_hour+":"+getMin(pickerSelectedValue.time_minute) ;
			
		
		setTitle(this,"Busses at "+ currentHour); 
    	
		
		
		setUpSchedueleList();
		
		
    	if(!this.schedueleList)
    		{
    			this.schedueleList = this.render({
    				xtype:'SchedueleList',
    			});
    		}
    	
    	var backBtn = this.application.viewport.query('#backBtn')[0];
  	    backBtn.show();
  	    backBtn.setHandler(function()
				{ 
  	    	backBtn.hide();
  	    	cleanAll();
  	    	Ext.dispatch({
				controller: 'Busses',
				 action: 'show2',
				 historyUrl: 'Busses/show2', 
				 //
				 animation: {
					 type: 'slide',
					 reverse: true,
				 },
  	    	 });
	 });
  	    
  	    
  	   var nextBtn = this.application.viewport.query('#nextBtn')[0];
	   nextBtn.hide();
	   nextBtn.setHandler(function()
				{
 		  		Ext.dispatch({
 		  					controller: 'Home',
 		  					 action: 'index',
 		  					 historyUrl: 'Home/index', 
 		  					 //
 		  					 animation: {
				            type: 'slide',
				            reverse: true,
				         },
				    });
				});
	   this.application.viewport.setActiveItem(this.schedueleList );
	  
},
});


function cleanAll(){
	//stop dispatcher 
	  clearInterval ( hoursInterval );
	//clear panel
	listScheduelePanel.removeAll();
	listScheduelePanel.doLayout();
	//listScheduelePanel = null;
	//clear list
	var l= schedueleList.length;
	  for(var i=0; i<l  ;i++){
		  var tmp = schedueleList[i] ;
		  schedueleList.remove(tmp);
	  }
	  schedueleList = [];
}